package com.ruoyi.station.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.station.domain.SDeviceProperties;
import com.ruoyi.station.domain.SMachineRoom;
import com.ruoyi.station.domain.SStation;
import com.ruoyi.station.mapper.SMachineRoomMapper;
import com.ruoyi.station.mapper.SStationMapper;
import com.ruoyi.station.service.SMachineRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 机房Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-05
 */
@Slf4j
@Service
public class SMachineRoomServiceImpl extends ServiceImpl<SMachineRoomMapper, SMachineRoom> implements SMachineRoomService {

    @Autowired
    private SMachineRoomMapper sMachineRoomMapper;
    @Autowired
    private SStationMapper sStationMapper;
    /**
     * 导入机房数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importMachineRoom(MultipartFile file) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readRoom : lists) {
            //机房名称
            String roomName = String.valueOf(readRoom.get(1)).trim();
            //所属站点
            String sStationName = String.valueOf(readRoom.get(2)).trim();
            SStation sStation = sStationMapper.selectOne(new LambdaQueryWrapper<SStation>().eq(SStation::getStationName, sStationName).eq(SStation::getCreateBy,user.getUserName()));
            if (sStation == null ) return "第" + i +"行站点不存在！";
            //机房类型
            String machineRoomType = DictUtils.getDictValue("machine_room_type", String.valueOf(readRoom.get(3)).trim());
            if(StringUtils.isBlank(machineRoomType)) return "第" + i +"行机房类型不存在！";

            //机房编号
            String machineRoomNum = String.valueOf(readRoom.get(4)).trim();
            if(StringUtils.isBlank(machineRoomNum)) return "第" + i +"行机房编号不正确！";

            //机房位置
            String machineRoomPlace = String.valueOf(readRoom.get(5)).trim();
            if(StringUtils.isBlank(machineRoomPlace)) return "第" + i +"行机房位置不正确！";

            //走线方式
            String takeUpMode = DictUtils.getDictValue("take_up_mode", String.valueOf(readRoom.get(6)).trim());
            if(StringUtils.isBlank(takeUpMode)) return "第" + i +"行走线方式不存在！";

            //最大屏位数
            String maxScreenNumber = String.valueOf(readRoom.get(7)).trim();
            if(StringUtils.isBlank(maxScreenNumber)) return "第" + i +"行最大屏位数不正确！";

            //已用屏位数
            String useScreenNumber = String.valueOf(readRoom.get(8)).trim();
            if(StringUtils.isBlank(useScreenNumber)) return "第" + i +"行已用屏位数不正确！";

            //是否监控
            String monitoringFlag = DictUtils.getDictValue("monitoring_flag", String.valueOf(readRoom.get(9)).trim());
            if(StringUtils.isBlank(monitoringFlag)) return "第" + i +"行是否监控不存在！";

            SMachineRoom build = SMachineRoom.builder()
                    .machineRoomName(roomName)
                    .station(sStation.getId())
                    .machineRoomType(machineRoomType)
                    .machineRoomNum(machineRoomNum)
                    .machineRoomPlace(machineRoomPlace)
                    .takeUpMode(takeUpMode)
                    .maxScreenNumber(maxScreenNumber)
                    .useScreenNumber(useScreenNumber)
                    .monitoringFlag(monitoringFlag)
                    .fillBy(user.getUserName())
                    .fillById(String.valueOf(user.getUserId()))
                    .fillDept(user.getDept().getDeptName())
                    .fillDeptId(user.getDeptId())
                    .createBy(user.getUserName())
                    .createTime(DateUtil.date())
                    .build();
            int insert = sMachineRoomMapper.insert(build);
            if (insert < 1) return "第" + i +"行保存出错了！";
            i++;
        }
        return "成功导入"+lists.size()+"条";
    }
}
